package com.zm.damai.damaiuser.web;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rabbitmq")
public class MqDo {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("workMsg")
    public String workMsg(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("workQueue",msg + " " + i);
        }
        return "工作模式消息";
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void workQueue(String msg){
        System.out.println("消费方法1： " + msg);
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void workQueue2(String msg){
        System.out.println("消费方法2： " + msg);
    }


    @RequestMapping("fanoutMsg")
    public String fanoutMsg(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("fanoutEx","",msg + " " + i);
        }
        return "广播模式消息";
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue1"),
            exchange = @Exchange(name = "fanoutEx",type = ExchangeTypes.FANOUT)
    ))
    public void fanoutQueue(String msg){
        System.out.println("广播模式消费方法1： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanoutQueue2"),
            exchange = @Exchange(name = "fanoutEx",type = ExchangeTypes.FANOUT)
    ))
    public void fanoutQueue2(String msg){
        System.out.println("广播模式消费方法2： " + msg);
    }



    @RequestMapping("topicMsg")
    public String topicMsg(String msg,String key){
        rabbitTemplate.convertAndSend("topicEx",key,msg);
        return "主题模式消息";
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue1"),
            exchange = @Exchange(name = "topicEx",type = ExchangeTypes.TOPIC),
            key = "user.*"
    ))
    public void topicQueue(String msg){
        System.out.println("主题模式消费方法1： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue2"),
            exchange = @Exchange(name = "topicEx",type = ExchangeTypes.TOPIC),
            key = {"user.insert","order.#"}
    ))
    public void topicQueue2(String msg){
        System.out.println("主题模式消费方法2： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue3"),
            exchange = @Exchange(name = "topicEx",type = ExchangeTypes.TOPIC),
            key = {"user.#","order.update"}
    ))
    public void topicQueue3(String msg){
        System.out.println("主题模式消费方法3： " + msg);
    }

}
